<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="会议名称" prop="meetingTitle">
        <el-input
          v-model="queryParams.meetingTitle"
          placeholder="会议名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划开始时间" prop="plannedStartTime">
        <el-date-picker clearable
          v-model="queryParams.plannedStartTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择计划开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="计划开始时间结束" prop="plannedEndTime">
        <el-date-picker clearable
          v-model="queryParams.plannedEndTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择计划开始时间结束">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in sheet_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
    
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['day:sheet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['day:sheet:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['day:sheet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="sheetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="会议名称" align="center" prop="meetingTitle" />
      <el-table-column label="签到人数" align="center" prop="number" />
      <el-table-column label="计划开始时间" align="center" prop="plannedStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.plannedStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="计划开始时间结束" align="center" prop="plannedEndTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.plannedEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sheet_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['day:sheet:remove']">删除</el-button>
          <el-button link type="primary" icon="SignIn" @click="handleSignIn(scope.row)">签到</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 签到二维码弹窗 -->
    <el-dialog title="会议签到" v-model="signInDialogVisible" width="400px">
      <div v-if="qrCodeUrl">
        <img :src="qrCodeUrl" alt="签到二维码" style="width: 100%;" />
      </div>
      <template v-slot:footer>
      <span class="dialog-footer">
        <el-button @click="signInDialogVisible = false">关闭</el-button>
      </span>
    </template>
    </el-dialog>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup name="Sheet">
import { listSheet, getSheet, delSheet, addSheet, updateSheet } from "@/api/day/sheet";
import QRCode from "qrcode";
const { proxy } = getCurrentInstance();
const { sheet_status } = proxy.useDict('sheet_status');

const sheetList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const signInDialogVisible = ref(false);
const qrCodeUrl = ref("");
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    meetingTitle: null,
    number: null,
    plannedStartTime: null,
    plannedEndTime: null,
    status: null
  },
  rules: {
    meetingTitle: [
      { required: true, message: "通过会议申请的会议名称不能为空", trigger: "blur" }
    ],
    number: [
      { required: true, message: "签到人数不能为空", trigger: "blur" }
    ],
    plannedStartTime: [
      { required: true, message: "计划开始时间不能为空", trigger: "blur" }
    ],
    plannedEndTime: [
      { required: true, message: "计划开始时间结束不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会议签到列表 */
function getList() {
  loading.value = true;
  listSheet(queryParams.value).then(response => {
    sheetList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    meetingTitle: null,
    number: null,
    plannedStartTime: null,
    plannedEndTime: null,
    status: null
  };
  proxy.resetForm("sheetRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSignIn(row) {
  const signInUrl = `http://localhost:80/#/day/sheet/sign`;
  QRCode.toDataURL(signInUrl)
    .then(url => {
      qrCodeUrl.value = url;
      signInDialogVisible.value = true;
    })
    .catch(err => {
      console.error(err);
    });
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加会议签到";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getSheet(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改会议签到";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["sheetRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSheet(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addSheet(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除会议签到编号为"' + _ids + '"的数据项？').then(function() {
    return delSheet(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('day/sheet/export', {
    ...queryParams.value
  }, `sheet_${new Date().getTime()}.xlsx`)
}

getList();
</script>
